import 'package:flutter/material.dart';
import 'package:flutter_yishop/constants/Constants.dart';
import 'package:flutter_yishop/pages/home/Home.dart';
import 'package:flutter_yishop/pages/category/Category.dart';
import 'package:flutter_yishop/pages/find/Find.dart';
import 'package:flutter_yishop/pages/shopcar/ShopCar.dart';
import 'package:flutter_yishop/pages/me/Me.dart';
import 'package:flutter_yishop/utils/screen/ScreenAdapter.dart';

class Tabs extends StatefulWidget {
  final index;

  Tabs({this.index = 0});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _Tabs(index);
  }
}

class _Tabs extends State<Tabs> {
  //创建页面控制器
  PageController _pageController;

  int _currentIndex = 0;

  List<Widget> _pageList = [Home(), Category(),  ShopCar(isBack: false,), Me()];

  List _pageTitle = ["首页", "分类", "购物车", "我的"];

  _Tabs(index) {
    this._currentIndex = index;
  }

  @override
  void initState() {
    super.initState();
    //页面控制器初始化
    _pageController = new PageController(initialPage: this._currentIndex);
  }

  Widget _appBarWidget() {
    if (this._currentIndex == 0) {
      return AppBar(
        leading: IconButton(
          icon: Icon(
            Icons.center_focus_weak,
            color: Colors.black87,
          ),
          onPressed: (){

          },
        ),
        actions: <Widget>[
          IconButton(
              icon: Icon(
            Icons.message,
            color: Colors.black87,
          ),
          onPressed: (){

          },),
        ],
        // backgroundColor: Colors.redAccent,
        title: InkWell(
          child: Container(
            padding: EdgeInsets.fromLTRB(
                ScreenAdapter.width(20),
                ScreenAdapter.width(8),
                ScreenAdapter.width(20),
                ScreenAdapter.width(8)),
            child: Row(
              children: <Widget>[
                Icon(
                  Icons.search,
                  color: Colors.black38,
                  size: 20,
                ),
                SizedBox(
                  width: ScreenAdapter.width(30),
                ),
                Text(
                  "笔记本",
                  style: TextStyle(
                      color: Colors.black38, fontSize: ScreenAdapter.size(25)),
                ),
              ],
            ),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(15)),
                color: Colors.black12),
          ),
          onTap: () {
            Navigator.of(context).pushNamed(Constants.SEARCH);
          },
        )
        // Text(_pageTitle[this._currentIndex])
        ,
        elevation: 0,
      );
    }
    else if(this._currentIndex == 3){
    }
    else {
      return AppBar(
        // backgroundColor: Colors.redAccent,
        title: Text(_pageTitle[this._currentIndex]),
        elevation: 0,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenAdapter.init(context);
    return Scaffold(
      // appBar: _appBarWidget(),
      //必须用PageView加载不同的页面
      body: PageView(
        physics: NeverScrollableScrollPhysics(),//禁止滑动
        controller: this._pageController,
        children: this._pageList,
        onPageChanged: (index) {
          setState(() {
            this._currentIndex = index;
          });
        }, //_pageList[this._currentIndex]
      ),
      bottomNavigationBar: BottomNavigationBar(
        fixedColor: Colors.redAccent,
        type: BottomNavigationBarType.fixed,
        currentIndex: this._currentIndex,
        onTap: (int index) {
          setState(() {
            this._currentIndex = index;
            //页面控制器进行跳转
            this._pageController.jumpToPage(this._currentIndex);
          });
        },
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("首页")),
          BottomNavigationBarItem(
              icon: Icon(Icons.category), title: Text("分类")),
          // BottomNavigationBarItem(
          //     icon: Icon(Icons.find_in_page), title: Text("发现")),
          BottomNavigationBarItem(
              icon: Icon(Icons.add_shopping_cart), title: Text("购物车")),
          BottomNavigationBarItem(icon: Icon(Icons.near_me), title: Text("我的")),
        ],
      ),
    );
  }
}
